// import type { marked } from "marked";

import type { marked } from "marked";
import { escape_html } from "./escape_html";
//@ts-ignore
import { unescape_html } from "./unescape_html";

// const { Renderer } = marked;
// class myrenderer extends Renderer {
//     image(href: string | null, title: string | null, text: string): string {
//         // console.log(href, title, text);
//         var result = Renderer.prototype.image.call(this,href, title, text);
//         // console.log(result);
//         if (result.startsWith("<img src=")) {
//             result = result.replace("<img src=", "<img loading='lazy' src=");
//         }
//         return result;
//     }
// }
// const markdownoptions = { renderer: new myrenderer() };
//@ts-ignore

export async function escapemarkedunescape(
    data: string,
    opts: marked.MarkedOptions | undefined = {}
) {
    const markedmodule: typeof import("marked") = require("marked");
    // console.log([data]);
    // var markedmodule = await import("marked");

    // const markedmodule: typeof import("marked") = await lazyloadmarked();
    // console.log(markedmodule);
    const markedfun = markedmodule.marked;
    data = markedfun(escape_html(data), opts /* markdownoptions */);

    // const clean = sanitize(data);
    data = unescape_html(data);
    // console.log(data);
    return data;
}
// async function lazyloadmarked() {
//     //@ts-ignore
//     if (process.env.browser && !process.env.PRERENDER) {
//         console.log("cjs marked");
//         return await new Promise<typeof import("marked")>((s, j) => {
//             //@ts-ignore
//             require.ensure(
//                 ["marked"],
//                 (require: (arg0: "marked") => typeof import("marked")) => {
//                     s(require("marked"));
//                 },
//                 (error: any) => {
//                     j(error);
//                 }
//             );
//         });
//     } else {
//         console.log("esm marked");
//         return import("marked");
//     }
// }
